﻿using Microsoft.SharePoint.Administration;
using SPSDeploy.Extensions.Controllers;
using SPSDeploy.Extensions.Extensions;
using System.Xml.Linq;

namespace SPSDeploy.Extensions.Modules.Extract
{
    /// <summary>
    /// Service application proxy extract module
    /// </summary>
    internal class ServiceApplicationProxyModule : BaseModule
    {

        /// <summary>
        /// Initialization
        /// </summary>
        /// <param name="controller"></param>
        internal ServiceApplicationProxyModule(BaseController controller)
            : base(controller)
        {
        }

        /// <summary>
        /// Execute module
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        internal override XElement Execute(XElement parent)
        {
            SPFarm farm = SPFarm.Local;
            XElement result = new XElement("serviceApplicationProxies");
            foreach (SPServiceProxy serviceProxy in farm.ServiceProxies)
                foreach (SPServiceApplicationProxy applicationProxy in serviceProxy.ApplicationProxies)
                    if (ServiceApplicationProxyExtension.IncludeProxy(farm, applicationProxy))
                        result.Add(new XElement("serviceApplicationProxy",
                            new XAttribute("name", applicationProxy.DisplayName),
                            new XAttribute("type", applicationProxy.TypeName)));
            parent.Add(result);
            controller.OnProgressMessage("Service application proxy information extracted.");
            return result;
        }

    }
}